﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCenter.DataProvider;
using UCenter.Models;
using UCenter.Core.Service;
using UCenter.BusinessContract;
using UCenter.TemplateEngine;
using UCenter.BusinessContract.Enumeration;
using UCenter.ServiceContract;
using UCenter.Services.Extensions;

namespace UCenter.Services
{
    public class MessageService : ServiceBase, IMessageService
    {
        #region IMessageService Members

        public List<MessageContract> GetInboxMessage(int userId)
        {
            List<MessageContract> list = new List<MessageContract>();
            List<Message> messages = LoadRepository<IMessageRepository>().GetInBoxMessages(userId);
            foreach (Message m in messages)
            {
                list.Add(m.ConvertToContract());
            }
            return list;
        }

        public MessageContract InsertMessage(AccountContract currentUser, int toUserID, TemplateContract msgTemplate, MessageType msgType)
        {
            Message msg = new Message();
            msg.Content = TemplateService.Generate(msgTemplate);
            msg.FromUserID = currentUser.ID;
            msg.IsDeleted = false;
            msg.SentDate = DateTime.Now;
            msg.ToUserID = toUserID;
            msg.Type = (byte)msgType;
            msg.IsNew = true;
            Message message = this.LoadRepository<IMessageRepository>().Add(msg);
            return message.ConvertToContract();
        }

        public void UpdateStatus(int msgID, bool isNew)
        {
            LoadRepository<IMessageRepository>().ChangeStatus(msgID, isNew);
        }

        public void DeleteMessage(int msgID)
        {
            LoadRepository<IMessageRepository>().Delete(msgID);
        }

        #endregion
    }
}
